/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as _ from "lodash";
import * as React from "react";
import "./product-price.scss";

interface ProductPriceProps {
    price: number | string | undefined;
    size: number;
}

export default class ProductPrice extends React.Component<ProductPriceProps> {

    public render() {
        const {size} = this.props;
        const decimalSize = size / 1.5;
        const unitSize = size / 1.5;
        const price = _.toNumber(this.props.price);
        if (_.isInteger(price)) {
            return (
                <div className="product-price">
                    <span style={{fontSize: `${unitSize}px`}}>¥</span>
                    <span style={{fontSize: `${size}px`}}>{price}</span>
                    <span style={{fontSize: `${decimalSize}px`}}>.00</span>
                </div>
            );
        } else {
            const retailPrices = _.toString(price).split(".");
            return (
                <div className="product-price">
                    <span style={{fontSize: `${unitSize}px`}}>¥</span>
                    <span style={{fontSize: `${size}px`}}>{retailPrices[0]}</span>
                    <span style={{fontSize: `${decimalSize}px`}}>.{retailPrices[1]}</span>
                </div>
            );
        }
    }
}
